package com.yanyan.sannuomin.util;





import org.apache.shiro.crypto.hash.Md5Hash;

import java.security.MessageDigest;

import java.security.NoSuchAlgorithmException;
/**
 * 〈学习常用工具〉
 * FileName: MD5
 * Author:   Administrator
 * <author>     <time>       <version>     <desc>
 * 作者姓名     修改时间        版本号        描述
 * PH        2021/12/20 16:14    2021.2      常用工具
 * 我的QQ:   2567653439
 */
public final class MD5 {

    //加密
    public static String encrypt(String strSrc) {
        try {
            char hexChars[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8',
                    '9', 'a', 'b', 'c', 'd', 'e', 'f' };
            byte[] bytes = strSrc.getBytes();
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(bytes);
            bytes = md.digest();
            int j = bytes.length;
            char[] chars = new char[j * 2];
            int k = 0;
            for (int i = 0; i < bytes.length; i++) {
                byte b = bytes[i];
                chars[k++] = hexChars[b >>> 4 & 0xf];
                chars[k++] = hexChars[b & 0xf];
            }
            return new String(chars);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            throw new RuntimeException("MD5加密出错！！+" + e);
        }
    }

    //产生随机盐值
    public static String getRandomSalt() {
        String model = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
        StringBuffer salt = new StringBuffer();
        char[] m = model.toCharArray();
        for (int i = 0; i < 4; i++) {
            char c = m[(int) (Math.random() * 36)];
            salt = salt.append(c);
        }
        return salt.toString();
    }

    public static String getCode() {
        String model = "1234567890";
        StringBuffer salt = new StringBuffer();
        char[] m = model.toCharArray();
        for (int i = 0; i < 7; i++) {
            char c = m[(int) (Math.random() * model.length())];
            salt = salt.append(c);
        }
        return "9"+salt.toString();
    }

    public static  String[]  getsalt(String MD5){
        //调用方法得到随机盐
        String salt=getRandomSalt();
        //自定义hash散列
        int hash=1024;
        //加盐加散列
        Md5Hash md5Hash = new Md5Hash(MD5,salt,hash);
        return  new String[]{md5Hash.toHex(),salt+","+hash};
    }







}

